#include "Game.hpp"
#include "camera.hpp"
#include "chessbox.hpp"
#include "filter_new.hpp"
#include <iostream>
#include <opencv2/core/types.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include <string>

int main() {
  // 创建相机对象
  libcamcv::Camera camera;

  // 初始化相机，使用640x480分辨率和XRGB8888格式
  if (!camera.init(800, 800, libcamera::formats::XRGB8888)) {
    std::cerr << "Failed to initialize camera" << std::endl;
    return 1;
  }

  std::cout << "Camera initialized: " << camera.getCameraId() << std::endl;
  serial sr = serial();
  JingZiqiSimulator jzs;
  CircularBufferFilter filter;
  int init = 88;
  int i = 0;
  jzs.start_game();
  camera.setFrameCallback(
      [&sr, &init, &jzs, &filter, &i](const cv::Mat &frame) {
        if (frame.empty()) {
          std::cerr << "Received empty frame" << std::endl;
          return;
        }
        if (init > 0) {
          init--;
          std::cout << "初始化中，请稍等..." << init << std::endl;
          if (init > 68) {
            std::cout << frame.cols << "x" << frame.rows << std::endl;
            cv::Mat black_roi = frame(cv::Rect(11, 407, 25, 21));
            cv::Mat white_roi = frame(cv::Rect(678, 455, 38, 25));
            cv::imshow("black", black_roi);
            cv::imshow("white", white_roi);
            cv::Point2f thresholds = init_thresholds(black_roi, white_roi);
            filter.addData(thresholds, 1.0f); // 添加初始阈值
          }
          if (init == 0) {
            std::cout << "初始化完成" << std::endl;
            cv::Point2f the = filter.getFilteredPoint();
            findboxConfig::MIN_COLOR_THER = 95;
            findboxConfig::MAX_COLOR_THER = 140;
            std::cout << "黑色阈值: " << findboxConfig::MIN_COLOR_THER
                      << ", 白色阈值: " << findboxConfig::MAX_COLOR_THER
                      << std::endl;
          }
          return; // 等待初始化完成
        }
        cv::Mat img = frame.clone();
        img = img(cv::Rect(63, 404, 585, 390));
        // cv::imshow("img",img);
        // // 计算下2/3区域
        // int height = img.rows;
        // int startY = 2 * height / 5 + 38; // 从3/5处开始
        // int offsetRight = 188; // 右侧裁剪宽度，可根据需要调整
        // cv::Rect roi(0, startY, img.cols - offsetRight, height - startY);

        // // 提取下2/3区域
        // cv::Mat lowerRegion = img(roi).clone();

        // 只处理下2/3区域
        jzs.game_on_going(img);
        // jzs.chess_->processImage(img);
        // cv::imshow("img",img);
        // 显示原始图像和分割线
        // 显示处理区域
        // cv::imshow("Processing Region", lowerRegion);
        // std::string path = "capture_" + std::to_string(i) + ".png";
        // cv::imwrite(path, img);
        // i++;
        // std::cout << "保存图片到: " << path << std::endl;
        cv::waitKey(1);
      });

  // 开始捕获
  if (!camera.start()) {
    std::cerr << "Failed to start camera capture" << std::endl;
    return 1;
  }

  std::cout << "Camera started. Press Enter to exit..." << std::endl;
  std::cin.get();

  // 停止捕获
  camera.stop();

  std::cout << "Camera stopped" << std::endl;

  return 0;
}